package com.icez.baseutil

import android.content.Intent
import android.graphics.Bitmap
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.widget.Button
import android.widget.ImageView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import androidx.core.content.FileProvider.getUriForFile
import com.icez.base_util_library.*
import com.icez.base_util_library.model.SystemImgModel
import kotlinx.coroutines.*
import java.io.File


class MainActivity2 : AppCompatActivity() {

    val testResult = IntentUtil.instance.toTakePicture(activity = this) {p0,it->
        if (it != null) {
            GlobalScope.launch(Dispatchers.Main) {
                var imgLists: ArrayList<SystemImgModel>? = null
                withContext(Dispatchers.IO) {
                    LogUtil.E("p0: "+p0)

                    imgLists = FileUtil.instance.getAllImg(this@MainActivity2, mFilePath = p0, mContentUri = it)
                    LogUtil.E("扫描所有的图片：" + imgLists.toString())
                    val file = CompressUtil.instance.compressImageForWebp(this@MainActivity2,imgLists?.get(0)?.absPath?:"",30)
                    LogUtil.E("压缩成功: "+(file==null))

                }
                LoadImgUtil.instance.loadImg(
                    this@MainActivity2,
                    iv,
                    imgLists?.get(0)?.absPath.toString()
                )

            }

        }

    }

    private var iv:ImageView ?= null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)
        LogUtil.E("uuid: "+SystemUtil.instance.getUUID()+" 长度："+SystemUtil.instance.getUUID().length)
//        val getContext = IntentUtil.instance.toTakePicture(activity = this)

        val bchart = findViewById<BarChart>(R.id.bchart)
        val hList = ArrayList<String>()
        for(i in 0..5){
            hList.add(i.toString())
        }
        bchart.setHorizontalList(hList)
        val vList = ArrayList<Float>()
        for(i in 0..20){
            vList.add(i*1.0f)
        }
        bchart.setVerticalList(vList)

        val imgUrl = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp2.itc.cn%2Fq_70%2Fimages03%2F20210919%2Fe5d63095072c49d2b3e65395e37c4ed2.gif&refer=http%3A%2F%2Fp2.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1655084254&t=803592c7f6b10c0db32997b93f94ffbc"
        iv = findViewById<ImageView>(R.id.iv)
        GlobalScope.launch(Dispatchers.Main) {


            var bitmap: Bitmap?= null
            var imgLists:ArrayList<SystemImgModel> ?= null
            withContext(Dispatchers.IO){
//                val uri = getUriForFile(this@MainActivity2,"com.example.android.fileprovider", File(FileUtil.instance.getSDFile()?.absolutePath+File.separator+Environment.DIRECTORY_PICTURES))
//                val  uri = FileProvider.getUriForFile(this@MainActivity2,"com.example.android.fileprovider", File(FileUtil.instance.getSDFile()?.absolutePath+File.separator+Environment.DIRECTORY_PICTURES+File.separator+ "test.webp"))

//                imgLists = FileUtil.instance.getAllImg(this@MainActivity2)
                LogUtil.E("扫描所有的图片："+imgLists.toString())
                LogUtil.E("sd路径："+FileUtil.instance.getSDFile())

                //content://media/external/images/media/273010
//                bitmap = LoadImgUtil.instance.getBitmap(this@MainActivity2,"content://media/external/images/media/273009")
            }
            LoadImgUtil.instance.loadImg(this@MainActivity2,iv,imgLists?.get(0)?.uri.toString())
//            iv.setImageBitmap(bitmap)
        }


        val button = findViewById<Button>(R.id.button)
        button?.setOnClickListener {
//            LogUtil.E("视频与图片位置："+Environment.DIRECTORY_DCIM)
//            LogUtil.E("图片位置："+ File.separator)
//            testResult?.launch("com.example.android")
            val intent = Intent()
            IntentUtil.instance.toActivity<MainActivity>(this@MainActivity2,intent)
        }

        findViewById<Button>(R.id.share).setOnClickListener {

            ShareUtil.instance.textToSystem(this, "123", mShareAppType = ShareUtil.ShareAppType.EMAIL)
//            SystemUtil.instance.copyTextToClipboard(this, "2342352")
        }

//        WindowManagerUtil.instance.initAppGlobalWindowCofig(this)
//
//        val view = LayoutInflater.from(this).inflate(R.layout.window_layout, null, false)
////            windowManager.addView(view, layoutParam)
//        WindowManagerUtil.instance.addAppGlobalWindow(view)
    }
}